package Gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JSeparator;
import javax.swing.JComboBox;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.IOException;

import javax.swing.border.BevelBorder;
import javax.swing.DefaultComboBoxModel;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.xml.sax.SAXException;

import Database.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
public class CreateDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField tfPersonID;
	private JTextField tfLastName;
	private JTextField tfDoB;
	private JTextField tfPoB;
	private JTextField tfNationality;
	private JTextField tfAddress;
	private JTextField tfRqName;
	private JTextField tfLetters;
	private JTextField tfFirstName;
	private JTextField tfNumber;
	private JTextField tfDate;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			CreateDialog dialog = new CreateDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public CreateDialog() {
		setBounds(100, 100, 619, 345);
		getContentPane().setLayout(null);
		contentPanel.setBounds(0, 0, 582, 263);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel);
		contentPanel.setLayout(null);
		
		JLabel lblPersonId = new JLabel("Person ID");
		lblPersonId.setBounds(10, 11, 61, 14);
		contentPanel.add(lblPersonId);
		
		tfPersonID = new JTextField();
		tfPersonID.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				String a =  tfPersonID.getText();	
					if(a=="")
						JOptionPane.showMessageDialog(null, "Input personID");
					else
					{
						try {
							//XMLProvider.LoadXML();
							Person fn = XMLProvider.GetPersonbyID(a);
							tfFirstName.setText(fn.getfirstName());
							tfLastName.setText(fn.getlastName());
							tfDoB.setText(fn.getDoB());
							tfPoB.setText(fn.getPoB());
							tfNationality.setText(fn.getNationality());
							tfAddress.setText(fn.getAddress());
						} catch (XPathExpressionException
								| ParserConfigurationException | SAXException
								| IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						
					}
			}
		});
		tfPersonID.setBounds(125, 8, 86, 20);
		contentPanel.add(tfPersonID);
		tfPersonID.setColumns(10);
		
		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel.setBounds(269, 11, 313, 156);
		contentPanel.add(panel);
		panel.setLayout(null);
		
		JLabel lblFirstname = new JLabel("Firstname");
		lblFirstname.setBounds(10, 11, 59, 14);
		panel.add(lblFirstname);
		
		tfFirstName = new JTextField();
		tfFirstName.setEditable(false);
		tfFirstName.setBounds(71, 8, 78, 20);
		panel.add(tfFirstName);
		tfFirstName.setColumns(10);
		
		JLabel lblLastname = new JLabel("Lastname");
		lblLastname.setBounds(159, 11, 59, 14);
		panel.add(lblLastname);
		
		tfLastName = new JTextField();
		tfLastName.setEditable(false);
		tfLastName.setBounds(217, 8, 86, 20);
		panel.add(tfLastName);
		tfLastName.setColumns(10);
		
		JLabel lblDob = new JLabel("DoB");
		lblDob.setBounds(10, 36, 46, 14);
		panel.add(lblDob);
		
		tfDoB = new JTextField();
		tfDoB.setEditable(false);
		tfDoB.setBounds(70, 33, 78, 20);
		panel.add(tfDoB);
		tfDoB.setColumns(10);
		
		JLabel lblPob = new JLabel("PoB");
		lblPob.setBounds(10, 61, 46, 14);
		panel.add(lblPob);
		
		tfPoB = new JTextField();
		tfPoB.setEditable(false);
		tfPoB.setBounds(70, 58, 225, 20);
		panel.add(tfPoB);
		tfPoB.setColumns(10);
		
		JLabel lblNation = new JLabel("Nationality");
		lblNation.setBounds(10, 86, 59, 14);
		panel.add(lblNation);
		
		tfNationality = new JTextField();
		tfNationality.setEditable(false);
		tfNationality.setBounds(70, 83, 86, 20);
		panel.add(tfNationality);
		tfNationality.setColumns(10);
		
		JLabel lblAddress = new JLabel("Address");
		lblAddress.setBounds(10, 114, 59, 14);
		panel.add(lblAddress);
		
		tfAddress = new JTextField();
		tfAddress.setEditable(false);
		tfAddress.setBounds(70, 111, 165, 20);
		panel.add(tfAddress);
		tfAddress.setColumns(10);
		
		JLabel lblRequiredAction = new JLabel("Required Action");
		lblRequiredAction.setBounds(10, 42, 110, 14);
		contentPanel.add(lblRequiredAction);
		
		String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
		final JComboBox cbRequired = new JComboBox(petStrings);
		cbRequired.setModel(new DefaultComboBoxModel(new String[] {"Must be Arrested", "Notify to Check", "Prevent from Travelling", "Put on Probation", "Release"}));
		cbRequired.setToolTipText("");
		cbRequired.setBounds(112, 39, 147, 20);
		contentPanel.add(cbRequired);
		
		JLabel lblRequestingDepartment = new JLabel("Requesting Department ");
		lblRequestingDepartment.setBounds(10, 108, 159, 14);
		contentPanel.add(lblRequestingDepartment);
		
		tfRqName = new JTextField();
		tfRqName.setBounds(10, 126, 249, 20);
		contentPanel.add(tfRqName);
		tfRqName.setColumns(10);
		
		JLabel lblRequestingDepartmentLetter = new JLabel("Requesting department letters");
		lblRequestingDepartmentLetter.setBounds(7, 150, 235, 14);
		contentPanel.add(lblRequestingDepartmentLetter);
		
		tfLetters = new JTextField();
		tfLetters.setBounds(9, 167, 249, 20);
		contentPanel.add(tfLetters);
		tfLetters.setColumns(10);
		
		JLabel lblRequestingDepartmentNumber = new JLabel("Requesting department number");
		lblRequestingDepartmentNumber.setBounds(10, 191, 235, 14);
		contentPanel.add(lblRequestingDepartmentNumber);
		
		tfNumber = new JTextField();
		tfNumber.setBounds(9, 210, 249, 20);
		contentPanel.add(tfNumber);
		tfNumber.setColumns(10);
		
		JLabel lblDate = new JLabel("Requerst Date");
		lblDate.setBounds(10, 80, 86, 14);
		contentPanel.add(lblDate);
		
		tfDate = new JTextField();
		tfDate.setBounds(97, 77, 162, 20);
		contentPanel.add(tfDate);
		tfDate.setColumns(10);
		
		
		JPanel buttonPane = new JPanel();
		buttonPane.setBounds(0, 274, 582, 33);
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane);
		{
			JButton btnCreate = new JButton("Create");
			btnCreate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String a =  tfPersonID.getText();
					try {
						Person fn = XMLProvider.GetPersonbyID(a);
						Integer ra = cbRequired.getSelectedIndex()+1;
						
						String id  = XMLProvider.generateWantedID();
						Wanted wanted = new Wanted(id, fn.getID(),ra ,tfRqName.getText(), tfLetters.getText(), tfNumber.getText(), tfDate.getText());
						boolean create = XMLProvider.addWanted(wanted);
						if (create == true)
							JOptionPane.showMessageDialog(null, "Create Success!");
						else
							JOptionPane.showMessageDialog(null, "Create Fail!");
						
					} catch (XPathExpressionException
							| ParserConfigurationException | SAXException
							| IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
			btnCreate.setActionCommand("OK");
			buttonPane.add(btnCreate);
			getRootPane().setDefaultButton(btnCreate);
		}
		{
			JButton cancelButton = new JButton("Cancel");
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setVisible(false);
				}
			});
			cancelButton.setActionCommand("Cancel");
			buttonPane.add(cancelButton);
		}
		
	}
}
